﻿/***捐助信息求助****/

using System.Collections.Generic;
using System.Data;
using System;
using ZHDJ.MobileInterface.Common;

namespace ZHDJ.MobileInterface
{
    public partial class ATMWantMatchSave : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            ATMWantMatchSaveOutParam result = new ATMWantMatchSaveOutParam();
            ATMWantMatchSaveInParam param = new ATMWantMatchSaveInParam();
            string logMsg = "";
            bool isFlag = true; 

            try
            {
                param = inParam as ATMWantMatchSaveInParam;

                if (string.IsNullOrEmpty(param.CreatebyAddress) || string.IsNullOrEmpty(param.CreatebyContact) || string.IsNullOrEmpty(param.Description) 
                    ||   string.IsNullOrEmpty(param.UserID) || string.IsNullOrEmpty(param.TakeID))
                {
                    result.code = "0";
                    result.desc = "用户ID、求助ID、联系地址、联系方式、描述不能为空";

                    isFlag = false;
                }

                if (isFlag)
                {
                    if (string.IsNullOrEmpty(param.Purposes) || (param.Purposes != "0" && param.Purposes != "1" && param.Purposes != "2" && param.Purposes != "3"))
                    {
                        result.code = "0";
                        result.desc = "资助用途必须为：0 低保、残疾、特困等居民;1 自助申请的困难居民;2 指定用途;3 任意用途";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    if (param.Purposes == "2" && string.IsNullOrEmpty(param.PurposesDescription))
                    {
                        result.code = "0";
                        result.desc = "资助用途为指定用途时，用途说明不能为空";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    if (param.ThingCount <= 0)
                    {
                        result.code = "0";
                        result.desc = "数量必须大于0";

                        isFlag = false;
                    }
                }

                DateTime EndTimeDt = DateTime.Now;
                if (isFlag)
                {
                    try
                    {
                        EndTimeDt = Convert.ToDateTime(param.EndTime); 
                    }
                    catch
                    {
                        result.code = "0";
                        result.desc = "有效时间不是有效日期格式数据";

                        isFlag = false;
                    } 
                }

                DataSet dsUserInfo = null;
                if (isFlag)
                {
                    dsUserInfo = InterfaceHelper.GetUseInfo(param.UserID);

                    if (dsUserInfo == null || dsUserInfo.Tables[0].Rows.Count == 0)
                    {
                        result.code = "0";
                        result.desc = "用户ID不存在";

                        isFlag = false;
                    } 
                }
                 
                DataTable dtATMTake = null;
                if (isFlag)
                {
                    dtATMTake = ATMMassageCS.QueryTakeInfo(param.TakeID);

                    if (dtATMTake == null || dtATMTake.Rows.Count == 0)
                    {
                        result.code = "0";
                        result.desc = "求助ID不存在";

                        isFlag = false;
                    } 
                }

                if (isFlag)
                {
                    if (ATMMassageCS.ATMSaveAndTakeIsExists(param.TakeID))
                    {
                        result.code = "0";
                        result.desc = "该求助已经匹配，不能重复匹配";

                        isFlag = false;
                    } 
                }

                if (isFlag)
                {
                    bool rfResult;
                    string name = Convert.ToString(dtATMTake.Rows[0]["Name"]);
                    string thingType = Convert.ToString(dtATMTake.Rows[0]["ThingType"]);
                    string takeModus = Convert.ToString(dtATMTake.Rows[0]["TakeModus"]);
                    string brandID = Convert.ToString(dsUserInfo.Tables[0].Rows[0]["BranchID"]);
                    rfResult = ATMMassageCS.ATMWantMatchSave(param, name, thingType, takeModus, brandID); 
                }

                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "转移过程出现异常，多次出现请联系管理员";
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode + "-ATMWantMatchSave", SerializeHelper.JsonSerializer<ATMWantMatchSaveInParam>(param), logMsg);
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<ATMWantMatchSaveInParam, ATMWantMatchSaveOutParam>();
            else
                SafeProcessRequest<ATMWantMatchSaveInParam, ATMWantMatchSaveOutParam>();
        } 
    }

    #region 请求参数
    public class ATMWantMatchSaveInParam
    {   
        /// <summary>
        /// 求助ID
        /// </summary>
        public string TakeID
        { get; set; }

        /// <summary>
        /// 用户ID	string	必须
        /// </summary>
        public string UserID
        { get; set; } 

        /// <summary>
        /// 联系方式	string	
        /// </summary>
        public string CreatebyContact
        { get; set; }

        /// <summary>
        /// 联系地址	string
        /// </summary>
        public string CreatebyAddress
        { get; set; }  

        /// <summary>
        /// 数量	Int	必填
        /// </summary>
        public int ThingCount
        { get; set; }

        /// <summary>
        /// 有效时间	DateTime	必填
        /// </summary>
        public string EndTime
        { get; set; }

        /// <summary>
        /// 描述	String	必填
        /// </summary>
        public string Description
        { get; set; }

        /// <summary>
        /// 资助用途:0 低保、残疾、特困等居民;1 自助申请的困难居民;2 指定用途（请列明）;3 任意用途    必填 
        /// </summary>
        public string Purposes
        { get; set; }

        /// <summary>
        /// 用途说明
        /// </summary>
        public string PurposesDescription
        { get; set; }
    }
    #endregion

    #region 结果参数
    public class ATMWantMatchSaveOutParam
    {
        public ATMWantMatchSaveOutParam()
        {
            code = "1";
            desc = "操作成功";
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }
    }
    #endregion 
}